<template>
        <!-- 添加或修改厂区|生产车间l生产流水线对话框 -->
    <el-dialog :title="pd.title" v-model="pd.open" width="500px" append-to-body>
          <el-form ref="workShopLineRef" :model="pd.form" :rules="pd.rules" label-width="80px">

            <el-form-item label="父类" prop="parentId">
              <el-tree-select
                  v-model="pd.form.parentId"
                  :props="{ value: 'id', label: 'name', children: 'children' }"
                  value-key="id"
                  placeholder="请选择父id" 
                  check-strictly
              />
            </el-form-item>

            <el-form-item label="名称" prop="name">
              <el-input v-model="pd.form.name" placeholder="请输入名称" />
            </el-form-item>
            <el-form-item label="面积" prop="area">
              <el-input v-model="pd.form.area" placeholder="请输入面积" />
            </el-form-item>
            <el-form-item label="负责人" prop="userId">
              <el-input v-model="pd.form.userId" placeholder="请输入负责人" />
            </el-form-item>
            <el-form-item label="是否启用" prop="enableFlag">
              <el-select v-model="pd.form.enableFlag" placeholder="请选择是否启用">
                <el-option
                    v-for="dict in sys_is"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="pd.form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-form>
          <template #footer>
            <div class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="pd.open = false">取 消</el-button>
            </div>
          </template>
        </el-dialog>
</template>

<script setup name="WorkShopLineCurd">
import { delWorkShopLine, addWorkShopLine, updateWorkShopLine, listWorkShopLine } from "@/api/wms/workShopLine";
import { getCurrentInstance, reactive } from "vue";
const { proxy } = getCurrentInstance();
const { sys_is } = proxy.useDict('sys_is')
const emit = defineEmits(["ok"]);

const pd = reactive({
    open: false,
    title: "",
    operation: 0,
    form: {},
    workShopLineOptions: [],
    queryParams: {
        name: null,
        ancestors: null,
        userId: null,
        enableFlag: null,
        updateTime: null,
        params: []
    },
    rules: {
        parentId: [
            { required: true, message: "父id不能为空", trigger: "blur" }
        ],
        name: [
            { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        enableFlag: [
            { required: true, message: "是否启用不能为空", trigger: "change" }
        ],
    }
});

/** 提交按钮 */
async function submitForm() {
    if(pd.operation == 1){
      debugger
        await addWorkShopLine(pd.form).then(res =>{
            proxy.$modal.msgSuccess("新增成功");
        });
    } else if(pd.operation == 2){
        await updateWorkShopLine(pd.form).then(res =>{
            proxy.$modal.msgSuccess("修改成功");
        });
    }
    pd.open = false
    emit("ok", pd.operation)
}

function add(row,factoryId) {
    reset();
    pd.form = {...pd.form }
    if (row != null && row.id) {
        pd.form.parentId = row.id;
        if((row.ancestors).split(',').length == 2){
          pd.title = "添加生产车间";
        }else{
          pd.title = "添加生产流水线";
        }
        getTreeselect(row.parentId);
    } else {
        pd.form.parentId = factoryId;
        debugger
        pd.title = "添加生产车间"
    }
    pd.open = true;
    pd.operation = 1
}
/** 新增厂区按钮操作 */
function addFactory() {
    reset();
    pd.form = { ...pd.form }
    pd.form.parentId = 0;
    pd.title = "添加厂区"
    pd.open = true;
    pd.operation = 1
}

/** 重置按钮操作 */
function reset() {
  pd.form = {
    id: null,
    parentId: null,
    name: null,
    ancestors: null,
    area: null,
    userId: null,
    enableFlag: null,
    tenantId: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    params: {}
 };
 pd.operation = 0
 pd.title = ""
 proxy.resetForm("workShopLineRef")
}


/** 修改按钮操作 */
function update(row) {
    reset();
    pd.form = { ...pd.form, ...row }
    if (row != null) {
        pd.form.parentId = row.parentId;
    }
        getTreeselect(row.parentId);
        pd.open = true;
        pd.title = "修改厂区|生产车间l生产流水线";
        pd.operation = 2
}



/** 删除按钮操作 */
function del(id) {
    pd.operation = 3
    proxy.$modal.confirm('是否确认删除厂区|生产车间l生产流水线编号为"' + id + '"的数据项？').then(function () {
        return delWorkShopLine(id);
    }).then(() => {
        proxy.$modal.msgSuccess("删除成功");
        emit("ok",pd.operation)
    }).catch(() => { });
}

/** 查询厂区|生产车间l生产流水线下拉树结构 */
function getTreeselect(parentId) {
  listWorkShopLine({ parentId: parentId }).then(response => {
    pd.workShopLineOptions = proxy.handleTree(response.data, "id", "parentId");
    console.log(pd.workShopLineOptions)
  });
}


defineExpose({ add, update, del, addFactory });
</script>
